var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
// 引入中间件
const adminAuth = require('./middlewares/admin-auth')
const userAuth = require('./middlewares/user-auth')
const cors = require('cors')

require('dotenv').config()

// 前台路由文件
var indexRouter = require('./routes/index');//主页面
var categoryRouter = require('./routes/categories');//分类页面
var courseRouter = require('./routes/courses');//课程页面
var courseDetailRouter = require('./routes/coursedetail');//课程详情页面
var chapterRouter = require('./routes/chapters');//章节查询接口
var articleRouter = require('./routes/articles');//文章查询接口
var articleDetailRouter = require('./routes/articledetail');//单条文章查询接口
var settingRouter = require('./routes/settings');//系统信息查询接口
var searchRouter = require('./routes/serach');//课程搜索接口

var authRouter = require('./routes/auth');//普通用户接口
var userRouter = require('./routes/users');//用户登陆后信息的接口
var likeRouter = require('./routes/likes');//用户登陆后信息的接口



// 后台路由文件
var adminArticlesRouter = require('./routes/admin/articles');
var adminCategoriesRouter = require('./routes/admin/categories');
var adminSettingsRouter = require('./routes/admin/settings');
var adminUsersRouter = require('./routes/admin/users');
var adminCoursesRouter = require('./routes/admin/courses');
var adminChaptersRouter = require('./routes/admin/chapters');
var adminChartsRouter = require('./routes/admin/charts');
var adminAuthRouter = require('./routes/admin/auth');



var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// 必须use在路由前面 处理跨域 不加参数是默认所有跨域都允许
const corsOptions = {
    origin:[
        'http://127.0.0.1:5500',
        'http://127.0.0.1:9000',
    ]
}
app.use(cors(corsOptions))




// 应用前台路由
app.use('/', indexRouter);
app.use('/category', categoryRouter);
app.use('/course', courseRouter);
app.use('/coursedetail', courseDetailRouter);
app.use('/chapter', chapterRouter);
app.use('/article', articleRouter);
app.use('/articleDetail', articleDetailRouter);
app.use('/setting', settingRouter);
app.use('/search', searchRouter);
app.use('/auth',authRouter)
app.use('/user',userAuth,userRouter)
app.use('/like',userAuth,likeRouter)



// 应用后台路由和中间件
app.use('/admin/articles',adminAuth, adminArticlesRouter);
app.use('/admin/categories',adminAuth, adminCategoriesRouter);
app.use('/admin/settings',adminAuth, adminSettingsRouter);
app.use('/admin/users',adminAuth, adminUsersRouter);
app.use('/admin/courses',adminAuth, adminCoursesRouter);
app.use('/admin/chapters',adminAuth, adminChaptersRouter);
app.use('/admin/charts',adminAuth, adminChartsRouter);
app.use('/admin/auth', adminAuthRouter);

module.exports = app;
